package com.ztf.reg;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegBase {

    public static void main(String[] args) {
        /*
        //简单认识正则表达式概念
        p("abc".matches("..."));
        p("a2323".replaceAll("\\d","-"));
        Pattern p = Pattern.compile("[a-z]{3}");
        Matcher m = p.matcher("fgh");
        p(m.matches());
        p("fgh".matches("[a-z]{3}"));

        //初步认识 . * 前面出现0次以上   +前面匹配一次以上   ?前面出现0次或一次　
        p(1,"a".matches("."));
        p(2,"aa".matches("aa"));
        p(3,"aaaa".matches(("a*")));
        p(4,"aaaa".matches("a*"));
        p(5,"".matches("a*"));
        p(6,"aaaa".matches("a?"));
        p(7,"a".matches("a?"));
        p(8,"12312312312313".matches("\\d{3,100}"));
        p(9,"192.168.0.a".matches(("\\d{0,3}\\.\\d{0,3}\\.\\d{0,3}\\.[a-z]")));
        p(10,"192".matches("[0-2][0-9][0-9]"));

        //范围
        p(1,"a".matches("[abc]"));
        p(2,"b".matches("[^abc]"));//不是abc开头的
        p(3,"a".matches("[a-zA-Z]"));//或者
        p(4,"a".matches("[a-z]|[A-Z]"));//或者
        p(5,"a".matches("[a-z[A-Z]]"));//或者
        p(6,"R".matches("[A-Z&&[RFG]]"));

        //认识\s \w \d \
        p(1," \n\r\t".matches("\\s{4}")); //A whitespace character(空格字符): [ \t\n\x0B\f\r]
        p(2," ".matches("\\S"));//非空格字符
        p(3,"a_8".matches("\\w{3}"));//A word character: [a-zA-Z_0-9]
        p(4,"abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));//
        p(5,"\\".matches("\\\\"));

        //POSIX Style  POSIX character classes (US-ASCII only)
        p(1,"a".matches("\\p{Lower}"));
        //boundary\b
        p(2,"hello sir".matches("^h.*"));
        p(3,"hello sir".matches(".*ir$"));
        p(4,"hello  sir".matches("^h[a-z]{1,3}o\\b.*"));
        p(5,"hellosir".matches("^h[a-z]{1,3}o\\b.*"));
        //练习
        p(1,"aaa 8888c".matches(".*\\d{4}."));
        p(2,"aaa 8888c".matches(".*\\b\\d{4}."));
        p(2,"aaa8888c".matches(".*\\d{4}."));
        p(2,"aaa8888c".matches(".*\\b\\d{4}."));

        //192.168.0.12
        String  x = "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}";
        p(1,"127.0.0.1".matches(x));
        */
        //邮箱地址验证923822745@qq.com
      /*  String y = "[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+";
        p("923822745@qq.com".matches(y));*/

        //matches find lookingAt
	/*	Pattern p = Pattern.compile("\\d{3,5}[-]\\d{3,5}[-]\\d{3,5}[-]\\d{2,3}");
        Pattern p1 = Pattern.compile("\\d{3,5}\\w\\d{3,5}\\w\\d{3,5}\\w\\d{2,3}");
        Pattern p2 = Pattern.compile("\\d{3,5}");
		String s = "123-34345-234-00";
        String s1 = "123_34345_234_00";
		Matcher m = p.matcher(s);
		p(1,m.matches());
        Matcher m1 = p1.matcher(s1);
        p(2,m1.matches());
		p(3,"a_89".matches("\\w{3,6}"));
		m.reset();
		p(2,m.find());
		p(3,m.start() + "-" + m.end());
		p(4,m.find());
		p(5,m.start() + "-" + m.end());
		p(6,m.find());
		p(7,m.start() + "-+" + m.end());
		p(8,m.find());
		//p(m.start() + "-" + m.end());
		p(9,m.lookingAt());
		p(10,m.lookingAt());
		p(11,m.lookingAt());
		p(12,m.lookingAt());*/


		Pattern p=Pattern.compile("\\d+");
		Matcher m=p.matcher("22bb23");
		Pattern p1 =  m.pattern();//返回p 也就是返回该Matcher对象是由哪个Pattern对象的创建的
		System.out.println("Pattern="+p1);
    }

    public static void p(Object o){
        System.out.println(o);
    }

    public static void p(Integer i ,Object o){
        System.out.println("第"+i+"个"+String.valueOf(o));

    }
}
